//
//  MyCenterViewController.m
//  Yihubang
//
//  Created by hyu on 16/7/25.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "MyCenterViewController.h"
#import "MyCenterModel.h"
#import "MyCenterHeaderView.h"
#import "MyCenterGuideView.h"
#import "MyCenterGuideView.h"
@interface MyCenterViewController ()<MyCenterGuideViewDelegate>
@property (nonatomic ,strong) MyCenterModel * viewModel;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (nonatomic ,strong) MyCenterGuideView *myCenterGuideView;
@property (nonatomic ,strong) MyCenterHeaderView * headerView;


@end

@implementation MyCenterViewController
@synthesize viewModel = _viewModel;
@synthesize mTableView = _mTableView;
- (void)viewDidLoad {
    
//    [self.navigationController.navigationBar setHidden:YES];
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:NO];
    [self.viewModel bindTableView:self.mTableView];
    self.mTableView.tableHeaderView = self.headerView;
    self.headerView.backgroundColor =self.mTableView.backgroundColor;
    [self setBackGroundImg];
     @weakify(self)
    [self.headerView setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
        UIViewController *vct = [story instantiateViewControllerWithIdentifier:@"ModiSelfInformationVC"];
        [self pushViewController:vct animated:YES];
    }];

}
-(void)setBackGroundImg{
    UIImageView * backImg = [UIImageView new];
    [backImg setImage:[UIImage imageNamed:@"BG"]];
    [self.view insertSubview:backImg atIndex:0];
    @weakify(self)
    [backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 60));
    }];

}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
    NSString* CallHelpFirst = [userDefault objectForKey:MyCenterFirstKey];
    if (CallHelpFirst == nil) {
        [self addMyCenterGuideView];
        [userDefault setBool:YES forKey:MyCenterFirstKey];
        [userDefault synchronize];
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.viewModel RefurbishMyInfo];
}

-(void)addMyCenterGuideView{
    self.myCenterGuideView = [[UINib nibWithNibName:@"MyCenterGuideView" bundle:nil] instantiateWithOwner:self options:nil].firstObject;
    self.myCenterGuideView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.myCenterGuideView.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:self.myCenterGuideView];
}

-(void)settingContact{
    UIStoryboard *HelpStory = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
    BaseViewController * BaseViewController = [HelpStory instantiateViewControllerWithIdentifier:@"MyContactVC"];
    [self pushViewController:BaseViewController];
}

- (MyCenterHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [MyCenterHeaderView loadInstanceFromNib];
    }
    return _headerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)outLogin:(id)sender {
    [self showHUDWithString:@"退出登录..."];
    @weakify(self)
    HttpManagerCenter *httpService = [HttpManagerCenter sharedHttpManager];
    [[httpService logoutWithUid:[UserClient sharedUserClient].userId resultClass:nil] subscribeNext:^(BaseModel *model){
    } error:^(NSError *error) {
    } completed:^{
        @strongify(self)
        [[UserClient sharedUserClient] outLogin];
        [self hiddenHUDWithString:nil error:NO];
        [[SlideNavigationController sharedInstance] closeMenuWithCompletion:^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"stopTimerToUpdate" object:nil];
        }];
    }];

}

//- (void)bindViewModel{
//    [super bindViewModel];
//}

- (void)bindViewModel{
    [super bindViewModel];
    @weakify(self)
    [RACObserve(self.viewModel, userInfo) subscribeNext:^(id x) {
         @strongify(self)
        NSDictionary * info =self.viewModel.userInfo;
        [self.headerView.userHeadImage setImageWithURLString:[[info allKeys] containsObject:@"img_top"]?info[@"img_top"]: info[@"portrait"]  placeholderImage:nil];
        self.headerView.userName.text = [[info allKeys] containsObject:@"nikename"]?info[@"nikename"]: info[@"nickname"];
    }];
}
#pragma mark -- getter and setter

- (MyCenterModel *)viewModel{
    if (_viewModel==nil) {
        _viewModel = [[MyCenterModel alloc] initWithViewController:self];
    }
    return _viewModel;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
